#include "HtmlStyle.h"
#include <e32std.h>
#include "stringutility.h"

using namespace Util;
namespace Renderer
{
//px % auto
TLength& TLength::operator=(const TDesC& aDes)
{
	HBufC* pBuf = NULL;
	TRAPD(err,
	pBuf = HBufC::NewL(aDes.Length());)
	
	if(err != KErrNone)
		User::Leave(ERR_TRGBEX_PARSERGB);
	
	TPtr ptrBuf = pBuf->Des();
	ptrBuf.Copy(aDes);
	ptrBuf.Trim();

	const TUint16* ptr = ptrBuf.Ptr();
	TInt i = 0;
	TBuf<8> len;
	while(ptr[i] >= 0x30 && ptr[i] <= 0x39)
	{
		len.Append(&ptr[i],1);
		i++;

		if(i == pBuf->Length())
			break;
	}

	if(i < pBuf->Length())
	{
		if(ptr[i] == '%')
			iLengthUnit = Percent;
		else if(i+1 < pBuf->Length() && ptr[i] == 'p' && ptr[i+1] == 'x')
			iLengthUnit = Pixel;
		else if(i+3 < pBuf->Length() && ptr[i] == 'a' && ptr[i+1] == 'u' && ptr[i+2] == 't' && ptr[i+3] == 'o')
			iLengthUnit = Auto;
		else
			iLengthUnit = Auto;
	}
	else  //�޵�λ
		iLengthUnit = Pixel;

	TLex lex(len);
	lex.Val(iVal);

	delete pBuf;
	pBuf = NULL;

	return *this;
}

TLength& TLength::operator=(TInt aLen)
{
	iVal = aLen;
	iLengthUnit = Pixel;

	return *this;
}

TInt TLength::RealLength(TInt maxLen) const
{
	if(iLengthUnit == TLength::Pixel)
		return iVal;
	else if(iLengthUnit == TLength::Percent)
		return maxLen*iVal/100;
	else if(iLengthUnit == TLength::Auto)
		return 0;

	return 0;
}

TBorderStyle::TBorderStyle()
{
	iBorderStyle = TBorderStyle::none;
}

//px style color
TBorderStyle& TBorderStyle::operator=(const TDesC& aDes)
{
	CStringUtility* strUtil = CStringUtility::NewL(aDes);
	_LIT(KSeperator," ");
	TBuf<1> bufSep(KSeperator);
	while(!strUtil->Eof())
	{
		TPtrC ptr = strUtil->NextToken(bufSep);
		
		GetAttributeType(ptr);
	}

	delete strUtil;
	strUtil = NULL;

	return *this;
}

void TBorderStyle::GetAttributeType(const TDesC& aDes)
{
	_LIT(KText,"#");
	TBuf<8> buf(KText);
	if(aDes.Find(buf) != KErrNotFound)
	{
		iRgb.ParseRGBL(buf);
		return;
	}
	
	buf = _L("none");
	if(aDes.Find(buf) != KErrNotFound)
	{
		iBorderStyle = TBorderStyle::none;
		return;
	}

	buf = _L("hidden");
	if(aDes.Find(buf) != KErrNotFound)
	{
		iBorderStyle = TBorderStyle::hidden;
		return;
	}

	buf = _L("dotted");
	if(aDes.Find(buf) != KErrNotFound)
	{
		iBorderStyle = TBorderStyle::dotted;
		return;
	}

	buf = _L("dashed");
	if(aDes.Find(buf) != KErrNotFound)
	{
		iBorderStyle = TBorderStyle::dashed;
		return;
	}

	buf = _L("solid");
	if(aDes.Find(buf) != KErrNotFound)
	{
		iBorderStyle = TBorderStyle::solid;
		return;
	}

	buf = _L("bDouble");
	if(aDes.Find(buf) != KErrNotFound)
	{
		iBorderStyle = TBorderStyle::bDouble;
		return;
	}

	buf = _L("groove");
	if(aDes.Find(buf) != KErrNotFound)
	{
		iBorderStyle = TBorderStyle::groove;
		return;
	}

	buf = _L("ridge");
	if(aDes.Find(buf) != KErrNotFound)
	{
		iBorderStyle = TBorderStyle::ridge;
		return;
	}

	buf = _L("inset");
	if(aDes.Find(buf) != KErrNotFound)
	{
		iBorderStyle = TBorderStyle::inset;
		return;
	}

	buf = _L("outset");
	if(aDes.Find(buf) != KErrNotFound)
	{
		iBorderStyle = TBorderStyle::outset;
		return;
	}

	buf = _L("px");
	if(aDes.Find(buf) != KErrNotFound)
	{
		iLength = aDes;
		return;
	}

	buf = _L("%");
	if(aDes.Find(buf) != KErrNotFound)
	{
		iLength = aDes;
		return;
	}

	buf = _L("auto");
	if(aDes.Find(buf) != KErrNotFound)
	{
		iLength = aDes;
		return;
	}

	iLength = aDes;
}

HtmlStyle::HtmlStyle()
{
	iDisplay = DisplayNone;
	iFloat = FloatNone;
	iPosition = PositionStatic;
}

HtmlStyle::~HtmlStyle()
{

}

}